using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VABCOMP
    {
        public DataRow Convert2ABCOMP(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryABCOMP(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["ABOMANDANT"] = " ".PadRight(2 ); 
         dwReturn["ABOCOMPNR"] = " ".PadRight(0 ); 
         dwReturn["ABOLFDNR"] = " ".PadRight(0 ); 
         dwReturn["ABOAENZL"] = " ".PadRight(0 ); 
         dwReturn["ABOAENB"] = " ".PadRight(8 ); 
         dwReturn["ABOAEND"] = " ".PadRight(0 ); 
         dwReturn["ABONEUB"] = " ".PadRight(8 ); 
         dwReturn["ABONEUD"] = " ".PadRight(0 ); 
         dwReturn["ABOCAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["ABOCAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["ABOCAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["ABOCOMPKZ"] = " ".PadRight(1 ); 
         dwReturn["ABOABTKZA"] = " ".PadRight(1 ); 
         dwReturn["ABOABTKZB"] = " ".PadRight(1 ); 
         dwReturn["ABOTEXTSL"] = " ".PadRight(3 ); 
         dwReturn["ABOFILLER"] = " ".PadRight(49 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapABCOMP(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveABCOMP(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryABCOMP(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapABCOMP(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertABCOMP(dwLocal, ITXDBVERSION);
            }
            UpdateABCOMP(dwLocal);
        }
        public void InsertABCOMP(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO ABCOMP(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryABCOMP(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM ABCOMP WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM ABCOMP  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "ABCOMP";
            Return = Result;
            return Return;
        }
        public void DeleteABCOMP(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM ABCOMP   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateABCOMP(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE ABCOMP SET "
                    +"ABOMANDANT= :ABOMANDANT,"
                    +"ABOCOMPNR= :ABOCOMPNR,"
                    +"ABOLFDNR= :ABOLFDNR,"
                    +"ABOAENZL= :ABOAENZL,"
                    +"ABOAENB= :ABOAENB,"
                    +"ABOAEND= :ABOAEND,"
                    +"ABONEUB= :ABONEUB,"
                    +"ABONEUD= :ABONEUD,"
                    +"ABOCAUFKRS= :ABOCAUFKRS,"
                    +"ABOCAUFLFD= :ABOCAUFLFD,"
                    +"ABOCAUFPOS= :ABOCAUFPOS,"
                    +"ABOCOMPKZ= :ABOCOMPKZ,"
                    +"ABOABTKZA= :ABOABTKZA,"
                    +"ABOABTKZB= :ABOABTKZB,"
                    +"ABOTEXTSL= :ABOTEXTSL,"
                    +"ABOFILLER= :ABOFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("ABOMANDANT", OracleType.NVarChar).Value = dwData["ABOMANDANT"].ToString(); 
          or1.Parameters.Add("ABOCOMPNR", OracleType.NVarChar).Value = dwData["ABOCOMPNR"].ToString(); 
          or1.Parameters.Add("ABOLFDNR", OracleType.NVarChar).Value = dwData["ABOLFDNR"].ToString(); 
          or1.Parameters.Add("ABOAENZL", OracleType.NVarChar).Value = dwData["ABOAENZL"].ToString(); 
          or1.Parameters.Add("ABOAENB", OracleType.NVarChar).Value = dwData["ABOAENB"].ToString(); 
          or1.Parameters.Add("ABOAEND", OracleType.NVarChar).Value = dwData["ABOAEND"].ToString(); 
          or1.Parameters.Add("ABONEUB", OracleType.NVarChar).Value = dwData["ABONEUB"].ToString(); 
          or1.Parameters.Add("ABONEUD", OracleType.NVarChar).Value = dwData["ABONEUD"].ToString(); 
          or1.Parameters.Add("ABOCAUFKRS", OracleType.NVarChar).Value = dwData["ABOCAUFKRS"].ToString(); 
          or1.Parameters.Add("ABOCAUFLFD", OracleType.NVarChar).Value = dwData["ABOCAUFLFD"].ToString(); 
          or1.Parameters.Add("ABOCAUFPOS", OracleType.NVarChar).Value = dwData["ABOCAUFPOS"].ToString(); 
          or1.Parameters.Add("ABOCOMPKZ", OracleType.NVarChar).Value = dwData["ABOCOMPKZ"].ToString(); 
          or1.Parameters.Add("ABOABTKZA", OracleType.NVarChar).Value = dwData["ABOABTKZA"].ToString(); 
          or1.Parameters.Add("ABOABTKZB", OracleType.NVarChar).Value = dwData["ABOABTKZB"].ToString(); 
          or1.Parameters.Add("ABOTEXTSL", OracleType.NVarChar).Value = dwData["ABOTEXTSL"].ToString(); 
          or1.Parameters.Add("ABOFILLER", OracleType.NVarChar).Value = dwData["ABOFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
